package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomdevice.vo.RoomSdiotPayModeConfigVO;
import com.xiaoshuidi.cloud.module.rooms.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomSdiotPayModeConfigMapper;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomSdiotPayModeMappingMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotPayModeConfig;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomSdiotPayModeMapping;
import com.xiaoshuidi.cloud.module.rooms.service.RoomSdiotPayModeConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class RoomSdiotPayModeConfigServiceImpl extends ServiceImpl<RoomSdiotPayModeConfigMapper, RoomSdiotPayModeConfig> implements RoomSdiotPayModeConfigService {

    private final RoomSdiotPayModeConfigMapper roomSdiotPayModeConfigMapper;

    private final RoomSdiotPayModeMappingMapper roomSdiotPayModeMappingMapper;

    @Override
    public CommonResult<String> addRoomSdiotPayMode(RoomSdiotPayModeConfig roomSdiotPayModeConfig) {
        String payMode = roomSdiotPayModeConfig.getPayMode();
        String feeType = roomSdiotPayModeConfig.getFeeType();
        QueryWrapper<RoomSdiotPayModeConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_mode", payMode).eq("fee_type", feeType);
        List<RoomSdiotPayModeConfig> list = roomSdiotPayModeConfigMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            return CommonResult.error(ErrorCodeConstants.PAY_MODE_EXISTS);
        }
        roomSdiotPayModeConfigMapper.insert(roomSdiotPayModeConfig);

        return CommonResult.success("设备付费模式设置成功");
    }

    @Override
    public List<RoomSdiotPayModeConfigVO> findAll() {
        List<RoomSdiotPayModeConfigVO> list = new ArrayList<>();
        List<RoomSdiotPayModeConfig> roomSdiotPayModeConfigs = roomSdiotPayModeConfigMapper.selectList(null);
        for (RoomSdiotPayModeConfig roomSdiotPayModeConfig : roomSdiotPayModeConfigs) {
            Long id = roomSdiotPayModeConfig.getId();
            QueryWrapper<RoomSdiotPayModeMapping> qw = new QueryWrapper<>();
            qw.eq("pay_mode_config_id", id);
            //todo @黄光辉 只查询数据量的操作用count
            int size = roomSdiotPayModeMappingMapper.selectList(qw).size();
            RoomSdiotPayModeConfigVO vo = new RoomSdiotPayModeConfigVO();
            vo.setCreateTime(roomSdiotPayModeConfig.getCreateTime());
            vo.setPayMode(roomSdiotPayModeConfig.getPayMode());
            vo.setFeeType(roomSdiotPayModeConfig.getFeeType());
            vo.setRemark(roomSdiotPayModeConfig.getRemark());
            vo.setId(id);
            vo.setSize(size);

            list.add(vo);
        }
        return list;
    }

    @Override
    public CommonResult<String> delete(RoomSdiotPayModeConfig roomSdiotPayModeConfig) {
        Long id = roomSdiotPayModeConfig.getId();
        QueryWrapper<RoomSdiotPayModeMapping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_mode_config_id", id);
        List<RoomSdiotPayModeMapping> list = roomSdiotPayModeMappingMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            return CommonResult.success("此付费模式设置已应用到房源，不可删除！");
        }
        roomSdiotPayModeConfigMapper.deleteById(id);
        return CommonResult.success("此付费模式已删除");
    }
}
